﻿using System;
using System.Collections.Generic;
using System.Text;
using KPIS.DBM;
using System.Configuration;
using KPIS.GERP.GIMS.MODEL;
using System.Data;

namespace KPIS.GERP.GIMS.DAL
{
    public class D_CalPriceAccount
    {
        DBManager dbManager = new DBManager(DataProvider.MySQL, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();

        public IList<M_CalPriceAccount> GetList()
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_CalPriceAccount> infoList = new List<M_CalPriceAccount>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = " SELECT icm_cal_price_account_seq,cp.asset_code,cp.fiscal_period,start_date_cal_depreciation "
                        +" ,end_date_cal_depreciation,remark "
                        +" ,CONCAT(COALESCE( cp.asset_code,''),':',COALESCE(asset_name,'')) as asset_name "
                        +" ,concat(concat(CAST(day(fiscal_start_date) as char(2)),'/',CAST(month(fiscal_start_date) as char(2)),'/',CAST(year(fiscal_start_date) as char(4))) ,'  To  ',CAST(day(fiscal_end_date) as char(2)),'/',CAST(month(fiscal_end_date) as char(2)),'/',CAST(year(fiscal_end_date) as char(4))) as fiscal_name "
                        +" FROM icm_cal_price_account  cp  "
                        +" LEFT JOIN icm_asset ia ON cp.asset_code = ia.asset_code "
                        + " LEFT JOIN acm_mas_fiscal_period fc ON fc.fiscal_period= cp.fiscal_period "
                        + " WHERE  cp.record_status <> @DeleteRecord ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_CalPriceAccount info = new M_CalPriceAccount();
                        info.icm_cal_price_account_seq = Convert.ToInt32(drc[i]["icm_cal_price_account_seq"]);
                        info.asset_code = drc[i]["asset_code"].ToString();
                        info.fiscal_period = cn.ConvertNullInt(drc[i]["fiscal_period"].ToString());
                        info.start_date_cal_depreciatiion = Convert.ToDateTime(drc[i]["start_date_cal_depreciation"]);
                        info.end_date_cal_depreciation = Convert.ToDateTime(drc[i]["end_date_cal_depreciation"]);
                        info.remark = drc[i]["remark"].ToString();
                        info.asset_name = drc[i]["asset_name"].ToString();
                        info.fiscal_desc = drc[i]["fiscal_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public M_CalPriceAccount GetListFillBySeq(Nullable<Int32> icm_cal_price_account_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_CalPriceAccount info = new M_CalPriceAccount();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@icm_cal_price_account_seq", icm_cal_price_account_seq);

                strSQL = " SELECT icm_cal_price_account_seq,cp.asset_code,cp.fiscal_period,start_date_cal_depreciation "
                        + " ,end_date_cal_depreciation,remark "
                        + " ,CONCAT(COALESCE( cp.asset_code,''),':',COALESCE(asset_name,'')) as asset_name "
                        + " ,concat(concat(CAST(day(fiscal_start_date) as char(2)),'/',CAST(month(fiscal_start_date) as char(2)),'/',CAST(year(fiscal_start_date) as char(4))) ,'  To  ',CAST(day(fiscal_end_date) as char(2)),'/',CAST(month(fiscal_end_date) as char(2)),'/',CAST(year(fiscal_end_date) as char(4))) as fiscal_name "
                        + " FROM icm_cal_price_account  cp  "
                        + " LEFT JOIN icm_asset ia ON cp.asset_code = ia.asset_code "
                        + " LEFT JOIN acm_mas_fiscal_period fc ON fc.fiscal_period= cp.fiscal_period "
                        + " WHERE  cp.record_status <> @DeleteRecord "
                            + " and icm_cal_price_account_seq = @icm_cal_price_account_seq ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.icm_cal_price_account_seq = Convert.ToInt32(drc[0]["icm_cal_price_account_seq"]);
                    info.asset_code = drc[0]["asset_code"].ToString();
                    info.fiscal_period = cn.ConvertNullInt(drc[0]["fiscal_period"].ToString());
                    info.start_date_cal_depreciatiion = Convert.ToDateTime(drc[0]["start_date_cal_depreciation"]);
                    info.end_date_cal_depreciation = Convert.ToDateTime(drc[0]["end_date_cal_depreciation"]);
                    info.remark = drc[0]["remark"].ToString();
                    info.asset_name = drc[0]["asset_name"].ToString();
                    info.fiscal_desc = drc[0]["fiscal_name"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public void Insert_CalPriceAccount(M_CalPriceAccount info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@asset_code", info.asset_code);
                dbManager.AddParameters(1, "@fiscal_period", info.fiscal_period);
                dbManager.AddParameters(2, "@start_date_cal_depreciation", info.start_date_cal_depreciatiion);
                dbManager.AddParameters(3, "@end_date_cal_depreciation", info.end_date_cal_depreciation);
                dbManager.AddParameters(4, "@remark", info.remark);
                dbManager.AddParameters(5, "@record_status", info.record_status);
                dbManager.AddParameters(6, "@created_by", info.created_by);
                dbManager.AddParameters(7, "@created_when", info.created_when);

                strSQL = " INSERT INTO icm_cal_price_account "
                        + " (asset_code,fiscal_period,start_date_cal_depreciation,end_date_cal_depreciation "
                        + " ,remark,record_status,created_by,created_when) "
                        + " VALUES "
                        + " (@asset_code,@fiscal_period,@start_date_cal_depreciation,@end_date_cal_depreciation "
                        + " ,@remark,@record_status,@created_by,@created_when) ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update_CalPriceAccount(M_CalPriceAccount info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(9);
                dbManager.AddParameters(0, "@icm_cal_price_account_seq", info.icm_cal_price_account_seq);
                dbManager.AddParameters(1, "@asset_code", info.asset_code);
                dbManager.AddParameters(2, "@fiscal_period", info.fiscal_period);
                dbManager.AddParameters(3, "@start_date_cal_depreciation", info.start_date_cal_depreciatiion);
                dbManager.AddParameters(4, "@end_date_cal_depreciation", info.end_date_cal_depreciation);
                dbManager.AddParameters(5, "@remark", info.remark);
                dbManager.AddParameters(6, "@record_status", info.record_status);
                dbManager.AddParameters(7, "@updated_by", info.updated_by);
                dbManager.AddParameters(8, "@updated_when", info.updated_when);

                strSQL = " UPDATE icm_cal_price_account "
                        + " SET asset_code = @asset_code "
                        + " ,fiscal_period = @fiscal_period "
                        + " ,start_date_cal_depreciation = @start_date_cal_depreciation "
                        + " ,end_date_cal_depreciation = @end_date_cal_depreciation "
                        + " ,remark = @remark "
                        + " ,record_status = @record_status "
                        + " ,updated_by =@updated_by "
                        + " ,updated_when = @updated_when "
                        + " WHERE icm_cal_price_account_seq = @icm_cal_price_account_seq ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete_CalPriceAccount(M_CalPriceAccount info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);
                dbManager.AddParameters(0, "@icm_cal_price_account_seq", info.icm_cal_price_account_seq);
                dbManager.AddParameters(1, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", info.updated_by);
                dbManager.AddParameters(3, "@updated_when", info.updated_when);

                strSQL = " UPDATE icm_cal_price_account "
                        + " SET record_status = @DeleteRecord "
                        + " ,updated_by = @updated_by "
                        + " ,updated_when = @updated_when "
                        + " WHERE icm_cal_price_account_seq = @icm_cal_price_account_seq ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
